<?php

namespace app\common\model\Games;

use think\Model;

class Games extends Model
{
    protected $name = 'games';

    protected $updateTime = false; //关闭更新时自动写入

    protected $field = true; //过滤

    protected $append = [
        'thumbs_text',
        'address_text',
        'avatar_text'
    ];

    public function getAvatarTextAttr($value, $data)
    {
        $avatar = $data['avatar'] ?: '';

        if (empty($avatar) || !is_file('.' . $avatar)) {
            $avatar = '/assets/img/avatar.png';
        }

        // 返回一个带有域名的下载地址
        return request()->domain() . $avatar;
    }

    public function getThumbsTextAttr($value, $data)
    {
        $thumbs = $data['thumbs'] ?? ''; // 使用空合并运算符简化代码

        if (empty($thumbs)) {
            return []; // 返回空数组而非空字符串，保持类型一致性
        }

        // 如果是数组，先转为字符串
        if (is_array($thumbs)) {
            $thumbs = implode(',', $thumbs);
        }

        // 将字符串拆分为数组并拼接完整 URL
        return array_map(function ($path) {
            return request()->domain() . $path;
        }, explode(',', $thumbs));
    }

    public function getAddressTextAttr($value, $data)
    {
        $address = $data['address'] ?: '';
        if (empty($address)) {
            return '';
        }
        return request()->domain() . $address;
    }
}
